package com.projectxxxxxxxxxx.service;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.projectxxxxxxxxxx.RoboApplication;
import com.projectxxxxxxxxxx.utils.UiUtils;

public class mService1 extends Service {

	private final Handler objHandler = new Handler();
	private int intCounter = 0;
	private final int timeDelay = 1000;
	private int mHour;
	private int mMinute;
	private int mYear;
	private int mMonth;
	private int mDay;
	private String mdate;
    
	final public String DEV_FILE = "/proc/self/net/dev";
	//String[] localdata = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" };
	//String[] ethdata = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" };
	String[] gprsdata = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" };
	String[] wifidata = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" };

	String[] firstData = null;
	String[] lastData = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" };
	// String saveData = "0,0,0,0,0,0,0,0,0,0,0,0";

	// bd
	// id time data

	final String LOCALLINE = "    lo";
	final String ETHLINE = "  eth0";
	final String GPRSLINE = "rmnet0";
	final String WIFILINE = "tiwlan0";
      
	final String TEXT_ENCODING = "UTF-8";
      
	private final Runnable mTasks = new Runnable() {
		@Override
		public void run() {
			Log.e(getClass().toString(), RoboApplication.getMethodName());
			refresh();
			intCounter++;
			objHandler.postDelayed(mTasks, timeDelay);
       }   
    };  
    
	@Override
	public void onStart(Intent intent, int startId) {
		Log.e(getClass().toString(), RoboApplication.getMethodName());
		objHandler.postDelayed(mTasks, 0);
		super.onStart(intent, startId);
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {
		objHandler.removeCallbacks(mTasks);
		super.onDestroy();
	}

	public void DisplayToast(String str) {
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}
    
	public void readdev() {
		FileReader fstream = null;
		try {
			fstream = new FileReader(DEV_FILE);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}
		BufferedReader in = new BufferedReader(fstream, 500);
		String line;
		String[] segs;
		String[] netdata;

		int k;
		int j;
		try {
			while ((line = in.readLine()) != null) {
				segs = line.trim().split(":");

//				if (line.startsWith(LOCALLINE)) {
//					netdata = segs[1].trim().split(" ");
//					for (k = 0, j = 0; k < netdata.length; k++) {
//						if (netdata[k].length() > 0) {
//							localdata[j] = netdata[k];
//							j++;
//						}
//					}
				// } else if (line.startsWith(ETHLINE)) {
				// netdata = segs[1].trim().split(" ");
				// for (k = 0, j = 0; k < netdata.length; k++) {
				// if (netdata[k].length() > 0) {
				// ethdata[j] = netdata[k];
				// j++;
				// }
				// }
//				} else 
				if (line.startsWith(GPRSLINE)) {
					netdata = segs[1].trim().split(" ");
					for (k = 0, j = 0; k < netdata.length; k++) {
						if (netdata[k].length() > 0) {
							gprsdata[j] = netdata[k];
							j++;
						}
					}
				} else if (line.startsWith(WIFILINE)) {
					netdata = segs[1].trim().split(" ");
					for (k = 0, j = 0; k < netdata.length; k++) {
						if (netdata[k].length() > 0) {
							wifidata[j] = netdata[k];
							j++;
						}
					}
				}
			}
			fstream.close();
		} catch (IOException e) {
			DisplayToast(e.toString());
		}
	}


	public String[] getLastData() {
		return lastData;
	}

	public void setLastData(String[] data) {
		lastData = data;
	}

	public void refresh() {

		readdev();
		// without ethdata
		// saveData = localdata[0] + "," + localdata[1] + "," + localdata[8] + "," + localdata[9] + "," + gprsdata[0]
		// + ","
		// + gprsdata[1] + "," + gprsdata[8] + "," + gprsdata[9] + "," + wifidata[0] + "," + wifidata[1] + ","
		// + wifidata[8] + "," + wifidata[9];

		String[] newData = new String[] { gprsdata[0], gprsdata[1], gprsdata[8], gprsdata[9], wifidata[0], wifidata[1], wifidata[8], wifidata[9] };
		if (firstData == null) {
			firstData = newData;
			setLastData(newData);
		}

		String oldData[] = getLastData(); // .split(",")

		int[] delta = new int[12];

		delta[0] = Integer.parseInt(newData[0]) - Integer.parseInt(oldData[0]);
		delta[1] = Integer.parseInt(newData[1]) - Integer.parseInt(oldData[1]);
		delta[2] = Integer.parseInt(newData[2]) - Integer.parseInt(oldData[2]);
		delta[3] = Integer.parseInt(newData[3]) - Integer.parseInt(oldData[3]);
		delta[4] = Integer.parseInt(newData[4]) - Integer.parseInt(oldData[4]);
		delta[5] = Integer.parseInt(newData[5]) - Integer.parseInt(oldData[5]);
		delta[6] = Integer.parseInt(newData[6]) - Integer.parseInt(oldData[6]);
		delta[7] = Integer.parseInt(newData[7]) - Integer.parseInt(oldData[7]);

		// final Calendar c = Calendar.getInstance();
		// mYear = c.get(Calendar.YEAR);
		// mMonth = c.get(Calendar.MONTH) + 1;
		// mDay = c.get(Calendar.DAY_OF_MONTH);
		// mHour = c.get(Calendar.HOUR_OF_DAY);
		// mMinute = c.get(Calendar.MINUTE);
		// mdate = mYear + "-" + mMonth + "-" + mDay;
		// saveData = saveData + mdate;


		setLastData(newData);



		Log.i(getClass().toString(),
				" rmnet0 Receive:  " + UiUtils.humanReadableByteCount(delta[0] / (timeDelay / 1000), true) + "/s ");
		Log.i(getClass().toString(),
				" rmnet0 Transmit:  " + UiUtils.humanReadableByteCount(delta[2] / (timeDelay / 1000), true) + "/s ");
		Log.i(getClass().toString(),
				" tiwlan0 Receive:  " + UiUtils.humanReadableByteCount(delta[4] / (timeDelay / 1000), true) + "/s ");
		Log.i(getClass().toString(),
				" tiwlan0 Transmit:  " + UiUtils.humanReadableByteCount(delta[6] / (timeDelay / 1000), true) + "/s ");


		Log.i(getClass().toString(),
				" rmnet0 Receive total:  "
						+ UiUtils.humanReadableByteCount(Integer.parseInt(newData[0]) - Integer.parseInt(firstData[0]),
								true));
		Log.i(getClass().toString(),
				" rmnet0 Transmit total:  "
						+ UiUtils.humanReadableByteCount(Integer.parseInt(newData[2]) - Integer.parseInt(firstData[2]),
								true));
		Log.i(getClass().toString(),
				" tiwlan0 Receive total:  "
						+ UiUtils.humanReadableByteCount(Integer.parseInt(newData[4]) - Integer.parseInt(firstData[4]),
								true));
		Log.i(getClass().toString(),
				" tiwlan0 Transmit total:  "
						+ UiUtils.humanReadableByteCount(Integer.parseInt(newData[6]) - Integer.parseInt(firstData[6]),
								true));

	}
}  